package vn.co.thn.web.demo_app_engine.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vn.co.thn.web.demo_app_engine.bean.BeanBase;
import vn.co.thn.web.demo_app_engine.jpa.NewsFeed;
import vn.co.thn.web.demo_app_engine.model.NewFeedModel;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

@SuppressWarnings("serial")
public class NewFeedAdminController extends ControllerBase {
	private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(request, response);
		String query = request.getQueryString();
		
		if (query != null && query.trim().length() > 0) {
			if (request.getParameter("action") != null && request.getParameter("action").equals("delete")) {
				NewFeedModel model=new NewFeedModel();
				model.setBlobstoreService(blobstoreService);
				model.Delete(Long.parseLong( request.getParameter("key")));
				List<NewsFeed> lst = new NewFeedModel().getAll(NewsFeed.class);
				response.setContentType("text/html;charset=UTF-8");
				PrintWriter out = response.getWriter();
				if (lst.size() > 0) {
					out.println("<table border=\"1\">");
					for (NewsFeed obj : lst) {
						out.println("<tr>");
						out.println("<td>" + obj.getKey() + "</td>");
						out.println("<td>" + obj.getId() + "</td>");
						//out.println("<td>" + obj.getName() + "</td>");
						out.println("<td><img src=\" " + obj.getImage_Url() + " \"</td>");
						//out.println("<td>" + obj.getDelete() + "</td>");
						out.println("<td><a href=\"JavaScript:ajax('?action=delete&key=" + obj.getKey() + "','htmldata')\" >Delete</a></td>");
						out.println("<td><a href=\"?action=edit&key=" + obj.getKey() + "\">Edit</a></td>");
						out.println("</tr>");
					}

					out.println("</table>");
				} else {
					out.println("");
				}
				return;
			}
			Forward("admin_newfeed_update");
		}else{
			NewFeedModel model = new NewFeedModel();
			request.setAttribute("datalist", model.getAll(NewsFeed.class));
		Forward("admin_newfeed");
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(request, response);
		Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(request);
		 BlobKey blobKey = blobs.get("Image_Url");
		 String key_image=blobKey.getKeyString();
		NewFeedModel model=new NewFeedModel();
		int port =request.getServerPort();
		String host;
		if(port==80){
			host=	request.getServerName();
		}else{
			host=	request.getServerName()+":"+port;
		}
		try {
			NewsFeed newsFeed=BeanBase.RequestToObject(request, NewsFeed.class);
			newsFeed.setKey_image(key_image);
			newsFeed.setImage_Url(request.getScheme()+"://"+host+"/imageview?image_url="+key_image);
			model.Add(newsFeed);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Redirect("/admin/newfeed");
	}
}
